//
//  BHNewfeatureController.m
//  xinlangweibo0621
//
//  Created by boldseas on 2017/6/22.
//  Copyright © 2017年 renbohan. All rights reserved.
//

//RGB颜色
#define BHColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]


#import "BHNewfeatureController.h"
#import <Masonry.h>
#import "BHTabBarViewController.h"

#define BHNewFeatureCount 4

@interface BHNewfeatureController () <UIScrollViewDelegate>

@property (nonatomic, weak) UIPageControl *pageControl;

@property (nonatomic, weak) UIScrollView *scrollView;


@end

@implementation BHNewfeatureController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.bottom.right.mas_equalTo(self.view);
        
    }];
    

    
    [self.view layoutIfNeeded];
    
//    NSLog(@"scrollView--%@, pageControl--%@", scrollView, pageControl);
    
    CGFloat scrollW = self.scrollView.frame.size.width;
    
    for (int i = 0; i < BHNewFeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc]init];
        [self.scrollView addSubview:imageView];
        
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        
        NSUInteger count = self.scrollView.subviews.count - 1;
        
        [self.view layoutIfNeeded];
        
        if (self.scrollView.subviews.count == 1) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
                make.size.mas_equalTo(self.view);
            }];
            
            
        }else{
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsMake(0, scrollW * count, 0, scrollW)).priorityLow();
                make.size.mas_equalTo(self.view);
            }];
        }
        [self.view layoutIfNeeded];
        if (i == BHNewFeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    scrollView.contentSize = CGSizeMake(scrollW * BHNewFeatureCount, 0);
    scrollView.bounces = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    pageControl.numberOfPages = BHNewFeatureCount;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).multipliedBy(1.7);
    }];
    
    pageControl.currentPageIndicatorTintColor = BHColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = BHColor(189, 189, 189);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    double page = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pageControl.currentPage = (int)(page + 0.5);
}

- (void)setupLastImageView:(UIImageView *)imageView{
    imageView.userInteractionEnabled = YES;
    
    //分享按钮
    UIButton *shareBtn = [[UIButton alloc]init];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [imageView addSubview:shareBtn];
    [shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(imageView.mas_centerX);
        make.centerY.mas_equalTo(imageView.mas_centerY).multipliedBy(1.3);
        make.width.equalTo(imageView.mas_width).multipliedBy(0.5);
    }];
    shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //开始微博按钮
    UIButton *startBtn = [[UIButton alloc]init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [imageView addSubview:startBtn];
    [startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(imageView.mas_centerX);
        make.centerY.mas_equalTo(imageView.mas_centerY).multipliedBy(1.5);
        make.size.mas_equalTo(startBtn.currentBackgroundImage.size);
    }];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)shareClick:(UIButton *)shareBtn{
    shareBtn.selected = !shareBtn.isSelected;
}

- (void)startClick{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    BHTabBarViewController *tabbarController = [[BHTabBarViewController alloc]init];
    
    window.rootViewController = tabbarController;
}

@end





//    UIScrollView *scrollView = [[UIScrollView alloc]init];
//    scrollView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:scrollView];
//    self.scrollView = scrollView;
//
//    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(self.view);
//    }];
//
//    UIPageControl *pageControl = [[UIPageControl alloc]init];
//    pageControl.backgroundColor = [UIColor blueColor];
//    pageControl.numberOfPages = BHNewFeatureCount;
//    [self.view addSubview:pageControl];
//    self.pageControl = pageControl;
//
//    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.view);
//        make.centerY.mas_equalTo(self.view).multipliedBy(1.7);
//    }];
//
//    [self.view layoutIfNeeded];
//
////    NSLog(@"scrollView--%@, pageControl--%@", scrollView, pageControl);
//
//    CGFloat scrollW = self.scrollView.frame.size.width;
//    CGFloat scrollH = self.scrollView.frame.size.height;
////    NSLog(@"W==%f------H==%f",scrollW,scrollH);
//
//    for (int i = 0; i < BHNewFeatureCount; i ++) {
//        UIImageView *imageView = [[UIImageView alloc]init];
//        CGRect imageViewF = imageView.frame;
//        imageViewF.size.width = scrollW;
//        imageViewF.size.height = scrollH;
//        imageViewF.origin.y = 0;
//        imageViewF.origin.x = scrollW * i;
//        imageView.frame = imageViewF;
//
//        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
//        imageView.image = [UIImage imageNamed:name];
//        [scrollView addSubview:imageView];
//
//    }
//
//    scrollView.contentSize = CGSizeMake(scrollW * BHNewFeatureCount, 0);
//    scrollView.bounces = NO;
//    scrollView.pagingEnabled = YES;
//    scrollView.showsHorizontalScrollIndicator = NO;


